# Urho3DToolsConfigVersion.cmake - Custom version file with host platform compatibility checking

set(PACKAGE_VERSION "@URHO3D_VERSION@")

# Include platform tag generation module
include("${CMAKE_CURRENT_LIST_DIR}/Modules/PlatformTag.cmake")

# Host platform compatibility check for Urho3DTools
function(urho3d_tools_check_host_platform_compatibility_version)
    # Generate current host platform tag (HOST mode - checking where tools will run)
    rbfx_get_platform_tag(CURRENT_HOST_PLATFORM_TAG HOST)

    # Host platform tag that was used at build time (embedded during configuration)
    set(BUILD_TIME_PLATFORM_TAG "@RBFX_PLATFORM_TAG@")

    # Use relaxed compatibility check for tools
    rbfx_check_tool_tag_compatibility(TOOLS_COMPATIBLE
        "${CURRENT_HOST_PLATFORM_TAG}"
        "${BUILD_TIME_PLATFORM_TAG}")

    # Compare tags with relaxed check
    if(NOT TOOLS_COMPATIBLE)
        set(PACKAGE_VERSION_UNSUITABLE TRUE PARENT_SCOPE)
        set(PACKAGE_VERSION_COMPATIBLE FALSE PARENT_SCOPE)
        if(NOT Urho3DTools_FIND_QUIETLY)
            message(STATUS "Urho3DTools: Host platform tag mismatch detected.")
            message(STATUS "  Current host platform: ${CURRENT_HOST_PLATFORM_TAG}")
            message(STATUS "  Built for:             ${BUILD_TIME_PLATFORM_TAG}")
            message(STATUS "  Hint: The Urho3DTools package was built for a different host platform.")
            message(STATUS "        Tools must match the host system where they will be executed.")
        endif()
        return()
    endif()

    # Host platform is compatible, continue with version checking
    set(PACKAGE_VERSION_COMPATIBLE TRUE PARENT_SCOPE)
endfunction()

# Perform host platform compatibility check
urho3d_tools_check_host_platform_compatibility_version()

# Only do version checking if host platform is compatible
if(PACKAGE_VERSION_COMPATIBLE)
    # Check whether the requested PACKAGE_FIND_VERSION is compatible
    if("@URHO3D_VERSION@" MATCHES "^([0-9]+)\\.")
        set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
        if(PACKAGE_FIND_VERSION_RANGE)
            # both endpoints of the range must have the expected major version
            math(EXPR CVF_VERSION_MAJOR_NEXT "${CVF_VERSION_MAJOR} + 1")
            if (NOT PACKAGE_FIND_VERSION_MIN_MAJOR STREQUAL CVF_VERSION_MAJOR
                OR ((PACKAGE_FIND_VERSION_INCLUDE_MAX
                    AND NOT PACKAGE_FIND_VERSION_MAX_MAJOR STREQUAL CVF_VERSION_MAJOR)
                    OR (NOT PACKAGE_FIND_VERSION_INCLUDE_MAX
                    AND NOT PACKAGE_FIND_VERSION_MAX_MAJOR STREQUAL CVF_VERSION_MAJOR_NEXT)))
                set(PACKAGE_VERSION_COMPATIBLE FALSE)
            endif()
        else()
            if(NOT PACKAGE_FIND_VERSION_MAJOR STREQUAL CVF_VERSION_MAJOR)
                set(PACKAGE_VERSION_COMPATIBLE FALSE)
            endif()
        endif()
    else()
        set(PACKAGE_VERSION_COMPATIBLE FALSE)
    endif()

    # if the installed or the requested version are pre-release versions,
    # exact version match is required
    if("@URHO3D_VERSION@" MATCHES "(.*[-+].+)|([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)")
        set(CVF_VERSION_PRERELEASE TRUE)
    else()
        set(CVF_VERSION_PRERELEASE FALSE)
    endif()

    if(PACKAGE_FIND_VERSION MATCHES "(.*[-+].+)|([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)")
        set(CVF_VERSION_FIND_PRERELEASE TRUE)
    else()
        set(CVF_VERSION_FIND_PRERELEASE FALSE)
    endif()

    if((CVF_VERSION_PRERELEASE AND NOT CVF_VERSION_FIND_PRERELEASE) OR (CVF_VERSION_FIND_PRERELEASE AND NOT CVF_VERSION_PRERELEASE))
        set(PACKAGE_VERSION_COMPATIBLE FALSE)
    endif()

    if (PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
        set(PACKAGE_VERSION_EXACT TRUE)
    endif()
endif()
